package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import com.fastadmin.pojo.Role;
import com.fastadmin.pojo.RoleResource;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.builder.SQL;
import lombok.RequiredArgsConstructor;
import org.asynchttpclient.util.StringBuilderPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Repository
@RequiredArgsConstructor
@CacheConfig(cacheNames = "roleResource")
public class RoleResourceMapper {

    private final EasyJdbcTemplate easyJdbcTemplate;

    public Role findByUrl(String url) {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	t_role.* ");
        sql.append(" FROM ");
        sql.append(" 	t_resource, ");
        sql.append(" 	t_role, ");
        sql.append(" 	t_role_resource ");
        sql.append(" WHERE ");
        sql.append(" 	1 = 1 ");
        sql.append(" AND t_resource.id = t_role_resource.resource_id ");
        sql.append(" AND t_role.id = t_role_resource.role_id ");
        sql.append(" WHERE ");
        sql.append(" 	t_resource.url =? ");
        return easyJdbcTemplate.get(sql.toString(), Role.class, url);
    }

    /**
     * 根据t_roles 的key 查询 url
     *
     * @param roles
     * @return
     */

    @Cacheable(key = "'roles:' + #p0")
    public List<Map<String, Object>> findUrlByRolesKey(String roles) {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	t_resource.url ");
        sql.append(" FROM ");
        sql.append(" 	t_resource, ");
        sql.append(" 	t_role, ");
        sql.append(" 	t_role_resource ");
        sql.append(" WHERE ");
        sql.append(" 	t_role.id = t_role_resource.role_id ");
        sql.append(" AND t_resource.id = t_role_resource.resource_id ");
        sql.append(" AND  FIND_IN_SET(t_role.role_key,?) ");
        return this.easyJdbcTemplate.find(sql.toString(), roles);

    }

    /**
     *  "id": 11175,
     *             "parent": 11174,
     *             "text": "查看",
     *             "type": "menu",
     *             "state": {
     *                 "selected": false
     *             }
     * @param role_key
     * @return
     */
    public List<Map<String, Object>> findResourceByRole(String role_key) {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	t_resource.id as id, ");
        sql.append(" 	t_resource.pid as parent, ");
        sql.append(" 	t_resource.title as text ,");
        sql.append(" 	t_resource.onemenu as type ");
        sql.append(" FROM ");
        sql.append(" 	t_resource ");
      /*  sql.append(" 	t_role, ");
        sql.append(" 	t_role_resource ");
        sql.append(" WHERE ");
        sql.append(" 	t_role.id = t_role_resource.role_id ");
        sql.append(" AND t_resource.id = t_role_resource.resource_id ");*/
        /*  sql.append(" AND  FIND_IN_SET(t_role.role_key,?) ");*/
        return this.easyJdbcTemplate.find(sql.toString());


    }
    public void findByUserId(Long id) {

    }

    public List<String> findByRoleIds(String ids) {
        StringBuilder sql = StringBuilderPool.DEFAULT.stringBuilder();
        sql.append(" SELECT ");
        sql.append(" 	resource_id  ");
        sql.append(" FROM ");
        sql.append(" 	t_role_resource ");
        sql.append(" WHERE ");
        sql.append(" 	t_role_resource.role_id=? ");
        ArrayList<String> resource_id = this.easyJdbcTemplate.find(sql.toString(), ids).stream().collect(ArrayList::new, (l, v) -> l.add(v.get("resource_id").toString()), List::addAll);
        return resource_id;

    }

    @CacheEvict(allEntries = true)
    public int deleteByRoleId(Long id) {
        return this.easyJdbcTemplate.delete(SQL.BUILD().DELETE_FROM("t_role_resource").WHERE("role_id=?"), id);
    }

    @CacheEvict(allEntries = true)
    public Object save(RoleResource roleResource) {
        return this.easyJdbcTemplate.insertKey(roleResource);
    }

}
